package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.vo.TerminationDetailVo;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.ContractsMapper;
import com.ruoyi.business.domain.Contracts;
import com.ruoyi.business.service.IContractsService;
import java.util.Date;


/**
 * 合同信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-13
 */
@Service
public class ContractsServiceImpl implements IContractsService 
{
    @Autowired
    private ContractsMapper contractsMapper;

    /**
     * 查询合同信息
     * 
     * @param id 合同信息主键
     * @return 合同信息
     */
    @Override
    public Contracts selectContractsById(Long id)
    {
        return contractsMapper.selectContractsById(id);
    }

    /**
     * 查询合同信息列表
     * 
     * @param contracts 合同信息
     * @return 合同信息
     */
    @Override
    public List<Contracts> selectContractsList(Contracts contracts,Long userId)
    {
        return contractsMapper.selectContractsList(contracts,userId);
    }

    /**
     * 新增合同信息
     * 
     * @param contracts 合同信息
     * @return 结果
     */
    @Override
    public int insertContracts(Contracts contracts, Long userId)
    {
        return contractsMapper.insertContracts(contracts,userId);
    }

    /**
     * 修改合同信息
     * 
     * @param contracts 合同信息
     * @return 结果
     */
    @Override
    public int updateContracts(Contracts contracts)
    {
        return contractsMapper.updateContracts(contracts);
    }

    /**
     * 批量删除合同信息
     * 
     * @param ids 需要删除的合同信息主键
     * @return 结果
     */
    @Override
    public int deleteContractsByIds(Long[] ids)
    {
        return contractsMapper.deleteContractsByIds(ids);
    }

    /**
     * 删除合同信息信息
     * 
     * @param id 合同信息主键
     * @return 结果
     */
    @Override
    public int deleteContractsById(Long id)
    {
        return contractsMapper.deleteContractsById(id);
    }


    @Override
    public void updateContractStatus() {
        List<Contracts> contracts = selectContractsForStatusCheck();
        Date now = new Date();
        for (Contracts contract : contracts) {
            // 假设合同有结束日期字段endDate，根据实际情况调整
            if (contract.getEndDate().getTime() - now.getTime() < (30 * 24 * 60 * 60 * 1000)) {
                contract.setStatus("即将到期");
            } else if (contract.getEndDate().before(now)) {
                contract.setStatus("已过期");
            }
            contractsMapper.updateContract(contract);
        }
    }

    @Override
    public List<Contracts> selectContractsForStatusCheck() {
        return contractsMapper.selectContractsForStatusCheck();
    }

    @Override
    public List<SysUser> selectUser() {
       return contractsMapper.selectUserList();
    }

    @Override
    public TerminationDetailVo selectDetailById(Long id) {
        return contractsMapper.selectDetailById(id);
    }
}
